package com.mobilepay.moneypig.model.activity.group;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.githang.statusbar.StatusBarCompat;
import com.gyf.barlibrary.ImmersionBar;
import com.loonggg.rvbanner.lib.RecyclerViewBanner;
import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.config.GlideCornerTransform;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zaaach.citypicker.CityPickerActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xlm on 2017/9/2 0002
 */

public class GroupActivity extends BaseActivity {
    private RelativeLayout mRelativeTitle;
    private RecyclerView mRecycleview;
    private RecyclerViewBanner mRecycleBanner;
    private List<String> mImages = new ArrayList<>();
    private GroupAdapter mGroupAdapter;
    private NestedScrollView mScrollView;
    private int bannerHeight;
    private SmartRefreshLayout mRefresh;
    private LinearLayout mLinearLocation;
    private LinearLayout mGroupSearch;
    private LinearLayout ll_group_search;
    private LinearLayout mGroupTitle;
    private RelativeLayout mGroupback;

    private LinearLayout mLinearType, mLinearNear, mLinearSpread;
    private static final int REQUEST_CODE_PICK_CITY = 0;

    @Override
    protected void onSetContentView() {
        initImmersionBar();
        setContentView(R.layout.activity_group);
    }

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        ImmersionBar mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();
    }

    @Override
    protected void initViews() {
        mGroupback= (RelativeLayout) findViewById(R.id.group_back);
        ll_group_search = (LinearLayout) findViewById(R.id.ll_group_search);
        mGroupTitle = (LinearLayout) findViewById(R.id.group_title_bar);
        mGroupSearch = (LinearLayout) findViewById(R.id.group_search);
        mLinearLocation = (LinearLayout) findViewById(R.id.group_location);
        mLinearType = (LinearLayout) findViewById(R.id.group_type);
        mLinearNear = (LinearLayout) findViewById(R.id.group_near);
        mLinearSpread = (LinearLayout) findViewById(R.id.group_spread);
        mRefresh = (SmartRefreshLayout) findViewById(R.id.group_refresh);
        mScrollView = (NestedScrollView) findViewById(R.id.group_scroll);
        mRelativeTitle = (RelativeLayout) findViewById(R.id.toolbar);
        mRelativeTitle.setBackgroundColor(Color.parseColor("#00000000"));
        ImmersionBar.setTitleBar(this, mRelativeTitle);
        mRecycleview = (RecyclerView) findViewById(R.id.group_rv);
        mRecycleview.setNestedScrollingEnabled(false);
        mRecycleBanner = (RecyclerViewBanner) findViewById(R.id.group_banner);
        for (int i = 0; i < 10; i++) {
            mImages.add("http://tupian.enterdesk.com/2013/xll/008/20/8/5.jpg");
        }
        mRecycleBanner.setRvBannerData(mImages);
        mRecycleBanner.setOnSwitchRvBannerListener(new RecyclerViewBanner.OnSwitchRvBannerListener() {
            @Override
            public void switchBanner(int i, AppCompatImageView imageView) {
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                Glide.with(App.getApp())
                        .load(mImages.get(i))
                        .placeholder(R.mipmap.default_lb)
                        .into(imageView);
            }
        });
        ViewGroup.LayoutParams bannerParams = mRecycleBanner.getLayoutParams();
        ViewGroup.LayoutParams titleBarParams = mGroupTitle.getLayoutParams();
        bannerHeight = bannerParams.height - titleBarParams.height - ImmersionBar.getStatusBarHeight(getThis());
    }


    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        setAdapter();
    }

    //重写onActivityResult方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_CITY && resultCode == RESULT_OK) {
            if (data != null) {
                String city = data.getStringExtra(CityPickerActivity.KEY_PICKED_CITY);
//              resultTV.setText("当前选择：" + city);
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onSetListener() {
        mGroupback.setOnClickListener(this);
        mLinearLocation.setOnClickListener(this);
        mLinearType.setOnClickListener(this);
        mLinearNear.setOnClickListener(this);
        mLinearSpread.setOnClickListener(this);
        mScrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY <= 0) {
                    ll_group_search.setEnabled(true);
                    mGroupSearch.setVisibility(View.GONE);
                    StatusBarCompat.setStatusBarColor(getThis(), Color.parseColor("#00000000"));
                    mGroupTitle.setBackgroundColor(Color.parseColor("#00000000"));
                } else if (scrollY > 0 && scrollY < bannerHeight) {
                    ll_group_search.setEnabled(true);
                    float scale = (float) scrollY / bannerHeight;
                    float alpha = 255 * scale;
                    StatusBarCompat.setStatusBarColor(getThis(), Color.argb((int) alpha, 228, 102, 65));
                    mGroupTitle.setBackgroundColor(Color.argb((int) alpha, 228, 102, 65));
                    mGroupSearch.setVisibility(View.GONE);
                } else {
                    mGroupSearch.setVisibility(View.VISIBLE);
                    ll_group_search.setEnabled(false);
                    StatusBarCompat.setStatusBarColor(getThis(), Color.parseColor("#e46641"));
                    mGroupTitle.setBackgroundColor(Color.parseColor("#e46641"));
                }
            }
        });
        mRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mRefresh.finishRefresh(1000, true);
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.group_type:
                startActivity(GroupTypeActivity.class, false, false);
                break;
            case R.id.group_near:
                break;
            case R.id.group_spread:
                break;
            case R.id.group_location:
                //启动
                startActivityForResult(new Intent(GroupActivity.this, CityPickerActivity.class), REQUEST_CODE_PICK_CITY);
                break;
            case R.id.group_back:
                finish();
                break;
        }
    }

    private void setAdapter() {
        mGroupAdapter = new GroupAdapter(this);
        mRecycleview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mGroupAdapter.setData(mImages);
        mRecycleview.addItemDecoration(new RecyclerView.ItemDecoration() {
            int space = CommonUtil.dip2px(8, GroupActivity.this);

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.left = space;
                outRect.right = space;
                outRect.bottom = space;
                // Add top margin only for the first item to avoid double space between items
                if (parent.getChildPosition(view) == 0)
                    outRect.top = space;
            }
        });
        mRecycleview.setAdapter(mGroupAdapter);
    }

    private class GroupAdapter extends RecyclerView.Adapter<GroupViewHolder> {

        private LayoutInflater inflater;
        private List<String> mImages;

        private GroupAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        public void setData(List<String> mImages) {
            this.mImages = mImages;
        }

        @Override
        public GroupViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new GroupViewHolder(inflater.inflate(R.layout.item_group, parent, false));
        }

        @Override
        public void onBindViewHolder(GroupViewHolder holder, int position) {
            Glide.with(holder.itemView.getContext())
                    .load(mImages.get(position))
                    .transform(new GlideCornerTransform(holder.itemView.getContext(), 12))
                    .placeholder(R.mipmap.default_credit)
                    .into(holder.mImagview);
        }

        @Override
        public int getItemCount() {
            return mImages.size();
        }

    }

    static class GroupViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImagview;
        private TextView mTextTitle;
        private TextView mTextIntroduce;
        private TextView mTextBuy;

        private GroupViewHolder(View itemView) {
            super(itemView);
            mImagview = (ImageView) itemView.findViewById(R.id.group_cover);
            mImagview.setScaleType(ImageView.ScaleType.FIT_XY);
            mTextTitle = (TextView) itemView.findViewById(R.id.group_title);
            mTextIntroduce = (TextView) itemView.findViewById(R.id.group_introduce);
            mTextBuy = (TextView) itemView.findViewById(R.id.group_buy);
        }
    }

}
